package com.example.iotest.bio;


import java.io.*;

/**
 * BufferedInputStream读取时,先读取数据进自带的buf,再从这个buf中拷贝数据进自定义的buffer中,
 * 如果最后一次拷贝进buffer时,buf中剩余的数据不足以填满buffer,会导致未填满部分是上一次拷贝的数据
 *
 * BufferedOutputStream写出时,用自定义buffer先填满buf,再一次性写出,buf未满需要调用flush()写出
 *
 */
public class BufferedIOTest {
    public static void main(String[] args) throws IOException {
//        copyFile();
        test1();
    }

    public static void copyFile() throws IOException {
        File file=new File("src/main/resources/files/1.txt");
        FileInputStream fileInputStream=new FileInputStream(file);
        System.out.println(fileInputStream.available());
        BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream,10);
        FileOutputStream fileOutputStream = new FileOutputStream("src/main/resources/files/2.txt");
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream,10);
        byte[] buffer=new byte[7];
        int len=0;
        while ((len=bufferedInputStream.read(buffer))!=-1){
            bufferedOutputStream.write(buffer);
        }
        bufferedOutputStream.close();
        bufferedInputStream.close();
    }

    public static void test1() throws IOException {
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("src/main/resources/files/1.txt"));
        byte[] buffer=new byte[9];
        bufferedInputStream.read(buffer);
        System.out.println(new String(buffer,0,buffer.length));
    }
}
